package com.example.shop.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.shop.R;
import com.example.sjlibrary.base.BaseFragment;

/**
 * Created by Administrator on 2016/3/9.
 */
public abstract class SCBaseFragment extends BaseFragment {
    TextView mTvTitleBarCenter;
    TextView mTvTitleBarLeft;
    TextView mTvTitleBarRight;
    private View titleBar;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // view RealativeLayout(titlebar+view)  return RealativeLayout
        super.onCreateView(inflater, container, savedInstanceState);
        RelativeLayout relativeLayout=new RelativeLayout(mBaseActivity);
        titleBar = mLayoutInflater.inflate(R.layout.titlebar, relativeLayout, false);
        mTvTitleBarCenter= (TextView) titleBar.findViewById(R.id.tv_title_bar_center);
        mTvTitleBarLeft= (TextView) titleBar.findViewById(R.id.tv_title_bar_left);
        mTvTitleBarRight= (TextView) titleBar.findViewById(R.id.tv_title_bar_right);
        relativeLayout.addView(titleBar);
        View contentView=setContentView(container);
        relativeLayout.addView(contentView);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) contentView.getLayoutParams();
        layoutParams.addRule(RelativeLayout.BELOW, R.id.title_bar);
        contentView.setLayoutParams(layoutParams);
        return relativeLayout;
    }
    //设置要渲染的View---fragment呈现的View
    public abstract View setContentView(ViewGroup container);



    //动态的控制显示/隐藏哪一个
    public void setTitleCenter(String text, View.OnClickListener onClickListener) {
        titleBar.setVisibility(View.VISIBLE);
        mTvTitleBarCenter.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(text.trim())){
            mTvTitleBarCenter.setText(text);
        }
        if (onClickListener!=null){
            mTvTitleBarCenter.setOnClickListener(onClickListener);
        }
    }
    public void setTitleCenter(int textID, View.OnClickListener onClickListener) {
        String text=this.getString(textID);
        titleBar.setVisibility(View.VISIBLE);
        mTvTitleBarCenter.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(text.trim())){
            mTvTitleBarCenter.setText(text);
        }
        if (onClickListener!=null){
            mTvTitleBarCenter.setOnClickListener(onClickListener);
        }
    }

    public void setTitleCenter(int textID) {
        setTitleCenter(textID,null);
    }
    public void setTitleCenter(String text) {
        setTitleCenter(text,null);
    }



    public void setTvTitleBarLeft(String text) {
        setTvTitleBarLeft(text,null);
    }
    public void setTvTitleBarLeft(String text, View.OnClickListener onClickListener) {
        titleBar.setVisibility(View.VISIBLE);
        mTvTitleBarLeft.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(text.trim())){
            mTvTitleBarLeft.setText(text);
        }
        if (onClickListener!=null){
            mTvTitleBarLeft.setOnClickListener(onClickListener);
        }
    }




    public void setTvTitleBarRight(String text) {
        setTvTitleBarRight(text,null);
    }
    public void setTvTitleBarRight(String text, View.OnClickListener onClickListener) {
        titleBar.setVisibility(View.VISIBLE);
        mTvTitleBarRight.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(text.trim())){
            mTvTitleBarRight.setText(text);
        }
        if (onClickListener!=null){
            mTvTitleBarRight.setOnClickListener(onClickListener);
        }
    }

}
